package com.ruoyi.airspaceFlight.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.airspaceFlight.domain.AirspaceApply;
import com.ruoyi.airspaceFlight.domain.ReleaseFlyApply;
import com.ruoyi.airspaceFlight.domain.TLowairServiceFile;
import com.ruoyi.airspaceFlight.mapper.AirspaceApplyMapper;
import com.ruoyi.airspaceFlight.mapper.ReleaseFlyApplyMapper;
import com.ruoyi.airspaceFlight.mapper.TLowairServiceFileMapper;
import com.ruoyi.airspaceFlight.dto.resp.FlyApplyAndAirspaceAndFileVo;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.airspaceFlight.mapper.FlyApplyMapper;
import com.ruoyi.airspaceFlight.domain.FlyApply;
import com.ruoyi.airspaceFlight.service.IFlyApplyService;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 飞行计划申请Service业务层处理
 * 
 * @author zc
 * @date 2023-11-01
 */
@Service
public class FlyApplyServiceImpl implements IFlyApplyService 
{
    @Autowired
    private FlyApplyMapper flyApplyMapper;


    @Autowired
    private AirspaceApplyMapper airspaceApplyMapper;

    @Autowired
    private TLowairServiceFileMapper tLowairServiceFileMapper;

    @Autowired
    private ReleaseFlyApplyMapper ReleaseFlyApplyMapper;
    /**
     * 查询飞行计划申请
     * 
     * @param flyApplyId 飞行计划申请主键
     * @return 飞行计划申请
     */
    @Override
    public FlyApplyAndAirspaceAndFileVo selectFlyApplyByFlyApplyId(String flyApplyId)
    {

        FlyApply flyApply = flyApplyMapper.selectFlyApplyByFlyApplyId(flyApplyId);
        AirspaceApply airspaceApply = airspaceApplyMapper.selectAirspaceApplyByAirspaceApplyId(flyApply.getAirspaceApplyId());

        TLowairServiceFile tLowairServiceFile = new TLowairServiceFile();
        tLowairServiceFile.setBusinessId(airspaceApply.getAirspaceApplyId());
        //查询文件信息
        LambdaQueryWrapper<TLowairServiceFile> qw = new LambdaQueryWrapper<>();
        qw.eq(TLowairServiceFile::getBusinessId,airspaceApply.getAirspaceApplyId());
        List<TLowairServiceFile> tLowairServiceFiles = tLowairServiceFileMapper.selectList(qw);
        //注入vo
        FlyApplyAndAirspaceAndFileVo flyApplyAndAirspaceAndFileVo = new FlyApplyAndAirspaceAndFileVo();

        flyApplyAndAirspaceAndFileVo.setFlyApply(flyApply);
        flyApplyAndAirspaceAndFileVo.setAirspaceApply(airspaceApply);
        if (CollUtil.isNotEmpty(tLowairServiceFiles)){
            flyApplyAndAirspaceAndFileVo.setTLowairServiceFiles(tLowairServiceFiles);
        }

        return  flyApplyAndAirspaceAndFileVo;

    }

    /**
     * 查询飞行计划申请列表
     * 
     * @param flyApply 飞行计划申请
     * @return 飞行计划申请
     */
    @Override
    public List<FlyApply> selectFlyApplyList( FlyApply flyApply)
    {

        return flyApplyMapper.selectFlyApplyList(flyApply);
    }

    /**
     * 新增飞行计划申请
     * 
     * @param flyApply 飞行计划申请
     * @return 结果
     */
    @Override
    public int insertFlyApply(FlyApply flyApply)
    {

        //添加飞行申请编号
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = dateFormat.format(date);
        String flyApplyyCode = "FX"+dateString+ RandomUtil.randomString(4);
        flyApply.setFlyApplyCode(flyApplyyCode);
        //固定数据
        flyApply.setFlyApplyId(IdUtils.randomUUID());
        flyApply.setCreateTime(DateUtils.getNowDate());
        flyApply.setUpdateTime(DateUtils.getNowDate());
//        flyApply.setCreateBy(SecurityUtils.getUsername());
//        flyApply.setUpdateBy(SecurityUtils.getUsername());
        return flyApplyMapper.insertFlyApply(flyApply);
    }

    /**
     * 修改飞行计划申请
     * 
     * @param flyApply 飞行计划申请
     * @return 结果
     */
    @Override
    public int updateFlyApply(FlyApply flyApply)
    {
        flyApply.setUpdateTime(DateUtils.getNowDate());
        //        flyApply.setUpdateBy(SecurityUtils.getUsername());
        int i = flyApplyMapper.updateFlyApply(flyApply);
        FlyApply flyApply1 = flyApplyMapper.selectFlyApplyByFlyApplyId(flyApply.getFlyApplyId());
        if ((flyApply.getApprovalStatus() == 1)) {
            ReleaseFlyApply ReleaseFlyApply = new ReleaseFlyApply();
            BeanUtil.copyProperties(flyApply1,ReleaseFlyApply);
            ReleaseFlyApply.setReleaseStatus(0);
            ReleaseFlyApply.setFlyApplyId(flyApply.getFlyApplyId());
            System.out.println(ReleaseFlyApply);

            //生成放飞计划申请编号
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String dateString = dateFormat.format(date);
            String tReleaseFlyApplyCode = "FF"+dateString+ RandomUtil.randomString(4);
            ReleaseFlyApply.setReleaseApplyCode(tReleaseFlyApplyCode);
            //固定数据
            ReleaseFlyApply.setCreateTime(DateUtils.getNowDate());
            ReleaseFlyApply.setUpdateTime(DateUtils.getNowDate());
            ReleaseFlyApply.setReleaseApplyId(IdUtils.randomUUID());
//        ReleaseFlyApply.setCreateBy(SecurityUtils.getUsername());
//        ReleaseFlyApply.setUpdateBy(SecurityUtils.getUsername());
            ReleaseFlyApplyMapper.insertReleaseFlyApply(ReleaseFlyApply);
        }
        return i;
    }

    /**
     * 批量删除飞行计划申请
     * 
     * @param flyApplyIds 需要删除的飞行计划申请主键
     * @return 结果
     */
    @Override
    public int deleteFlyApplyByFlyApplyIds(String[] flyApplyIds)
    {
        return flyApplyMapper.deleteFlyApplyByFlyApplyIds(flyApplyIds);
    }

    /**
     * 删除飞行计划申请信息
     * 
     * @param flyApplyId 飞行计划申请主键
     * @return 结果
     */
    @Override
    public int deleteFlyApplyByFlyApplyId(String flyApplyId)
    {
        return flyApplyMapper.deleteFlyApplyByFlyApplyId(flyApplyId);
    }
}
